<template>
    <div class="app-container">
        <!-- 搜索表单 -->
        <el-form ref="queryRef" :model="queryParams" :inline=true label-width="100px" v-show="showSearch">
            <el-form-item label="商品单位名称" prop="name">
                <el-input placeholder="请输入商品单位" clearable v-model="queryParams.name" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜 索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重 置</el-button>
            </el-form-item>
        </el-form>

        <!-- 功能按钮栏 -->
        <el-row :gutter=10 class="mb8">
            <el-col :span=1.5>
                <el-button type="primary" plain icon="Plus" @click="handleAdd()">新 增</el-button>
            </el-col>
            <el-col :span=1.5>
                <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate(ids)">修
                    改</el-button>
            </el-col>
            <el-col :span=1.5>
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete(ids, names)">删
                    除</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <!-- 数据展示表格 -->
        <el-table :data="productUnitList" v-loading="loading" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="名称" prop="name" width="200" />
            <el-table-column label="创建时间" prop="createTime" />
            <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width"
                #default="scope">
                <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row.id)">修 改</el-button>
                <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row.id, scope.row.name)">删
                    除</el-button>
            </el-table-column>
        </el-table>
        <!-- 新增或修改商品单位对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="productUnitRef" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="商品单位名称" prop="name" label-width="120px">
                    <el-input v-model="form.name" placeholder="请输入商品单位名称" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm()">确 定</el-button>
                    <el-button @click="cancle()">取 消</el-button>
                </div>
            </template>
        </el-dialog>
        <!-- 分页组件 -->
        <pagination v-show="total > 0" layout="sizes,prev,pager,next,jumper,->,total,slot"
            v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total"
            :page-sizes="[10, 20, 50, 100]" @pagination="getList" />
    </div>
</template>



<script setup name="ProductUnit">
import { getCurrentInstance, reactive } from 'vue';
import { listProductUnit, addProductUnit, getProductUnitdetail, updateProductUnit, deleteProductUnit } from '@/api/base/productUnit.js';

const { proxy } = getCurrentInstance();

//记录总数
const total = ref(2);
//加载数据时的动态效果控制
const loading = ref(true)
//新增与修改的对话框弹出控制
const open = ref(false)
//新增与修改的对话框的名称
const title = ref("");
//批量操作的品牌id列表
const ids = ref([]);
//批量操作的品牌名称列表
const names = ref([]);
//单选控制
const single = ref(true);
//多选控制
const multiple = ref(true);
//定义隐藏搜索控制模型
const showSearch = ref(true);
//参数
const data = reactive({
    //分页查询参数
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        //根据商品单位名称查询
        name: null
    },
    //新增或修改时的表单
    form: {
        id: null,
        name: null
    },
    //form表单的校验规则
    rules: {
        name: [
            {
                required: true,
                message: "商品单位名称不能为空",
                trigger: "blur"
            },
            {
                min: 1,
                max: 10,
                message: "商品单位的长度需在1到10个字符之间",
                trigger: "blur"
            }
        ]
    }
})
//将data对象中的属性转换为可以单独使用的响应式数据
const { queryParams, form, rules } = toRefs(data);
//分页表格数据列表
const productUnitList = ref([]);

//根据动态条件分页查询
function getList() {
    loading.value = true;
    listProductUnit(queryParams.value).then(
        response => {
            productUnitList.value = response.rows;
            total.value = response.total;
            loading.value = false;
        }
    ).catch(
        error => {
            proxy.$modal.msgError("分页查询失败");
            console.log(error);
        }
    );
}
//根据 商品单位名称搜索
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}
// 重置分页查询请求参数
function resetQuery() {
    // queryParams.value.name = null;
    // queryParams.value.pageNum = 1;
    // queryParams.value.pageSize = 10;
    proxy.resetForm("queryRef");
    handleQuery();
}
// 表单重置
function reset() {
    form.value = {
        id: null,
        name: null
    };
    proxy.resetForm("productUnitRef");
}
//打开新建商品单位对话框
function handleAdd() {
    reset();
    open.value = true;
    title.value = "新增商品单位";
}
//打开修改商品单位对话框
function handleUpdate(id) {
    reset();
    open.value = true;
    title.value = "修改商品单位信息";
    getProductUnitdetail(id).then(
        response => {
            form.value.id = response.data.id;
            form.value.name = response.data.name;
        }
    );
}
//对话框提交
function submitForm() {
    proxy.$refs["productUnitRef"].validate(
        valid => {
            if (valid) {
                if (form.value.id != null) {
                    // 修改
                    updateProductUnit(form.value).then(
                        () => {
                            proxy.$modal.msgSuccess("修改成功");
                            open.value = false;
                            getList();
                        }
                    ).catch(
                        (error) => {
                            proxy.$modal.msgError("修改失败");
                            console.log(error);
                            open.value = false;
                        });
                } else {
                    // 新增
                    addProductUnit(form.value).then(
                        () => {
                            proxy.$modal.msgSuccess("新增成功");
                            open.value = false;
                            getList();
                        }
                    ).catch((error) => {
                        proxy.$modal.msgError("新增失败");
                        console.log(error);
                        open.value = false;
                    });
                }
            }
        }
    )
}
//对话框取消
function cancle() {
    open.value = false;
    reset();
}

//删除商品单位
function handleDelete(id, name) {
    if (id == null) proxy.$modal.msgError("请先选择商品单位")
    else {
        proxy.$modal.confirm("是否确认删除商品单位" + name).then(
            () => {
                return deleteProductUnit(id);
            }
        ).then(() => {
            getList();
            proxy.$modal.msgSuccess("删除成功");
        }).catch(() => {
            proxy.$modal.msgError("删除失败");
        })
    }

}
//多选框变化时
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    names.value = selection.map(item => item.name);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}




//执行查询品牌列表
getList();
</script>

<style></style>